//
//  SelectImageViewController.m
//  LazyCat
//
//  Created by Abner on 16/9/24.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "SelectImageViewController.h"
#import "SelectImageCell.h"

static NSString *LCSelectImageCellIden = @"LCSelectImageCellIden";

@interface SelectImageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSInteger _webImgCount; //网络图片数量
}
@property(nonatomic,strong)UICollectionView *collectionView;
/** 点击了的图片的当前index */
@property(nonatomic,assign)NSInteger currentSelectedRow;

/** 没有图片的提示界面 */
@property(nonatomic,strong)UIView *noDataView;

@end

@implementation SelectImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"商品图片" doBack:YES];
    self.view.backgroundColor = SetColorRGBValue(0xF9F9F9, 1.0);
    

    if (self.imgUrlArrs.count > 0) {
        self.currentSelectedRow = -10;
        [self getWebImgCount];
        [self creatCollectionView];
        _collectionView.hidden = NO;
        _noDataView.hidden = YES;
    }else{
        [self creatNoDataView];
        _collectionView.hidden = YES;
        _noDataView.hidden = NO;
    }
}
-(CGFloat)getWebImgCount
{
    _webImgCount = 0;
    if (self.imgUrlArrs.count > 0) {
        for (int i = 0; i<self.imgUrlArrs.count; i++) {
            if (![self.imgUrlArrs[i] isKindOfClass:[UIImage class]]) {
                _webImgCount++;
            }
        }
    }
    LCLog(@"%s---%zd",__func__,_webImgCount);
    return _webImgCount;
}
- (void)backAction:(UIButton *)btn
{
    if (self.imgUrlArrs.count > self.currentSelectedRow && self.currentSelectedRow != -10) {
        if ([self.imgUrlArrs[self.currentSelectedRow] isKindOfClass:[UIImage class]]) {
            if (self.SelectImageVCBlock) {
                LCLog(@"%s---%zd---%zd",__func__,_webImgCount,self.currentSelectedRow);

                self.SelectImageVCBlock(NO,nil,self.currentSelectedRow-_webImgCount,self.imgUrlArrs[self.currentSelectedRow],self.currentSelectedRow); //web图片在前，本地图片在后
            }
        }else{
            if (self.SelectImageVCBlock) {
                self.SelectImageVCBlock(YES,self.imgUrlArrs[self.currentSelectedRow],-10,nil,self.currentSelectedRow);
            }
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)creatNoDataView
{
    _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    _noDataView.backgroundColor = SetColorRGBValue(0xF9F9F9, 1.0);
    [self.view addSubview:_noDataView];
    
    CGFloat imgY = (ScreenHeight-64-125)/2;
    CGFloat imgX = (ScreenWidth-84)/2;
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(imgX, imgY, 84, 84)];
    imgV.contentMode = UIViewContentModeScaleToFill;
    [_noDataView addSubview:imgV];
    imgV.image = [UIImage imageNamed:@"select_pic_no"];
    
    UILabel *textLB = [[UILabel alloc]initWithFrame:CGRectMake(0, imgV.bottom+25, ScreenWidth, 16)];
    textLB.font = FontSet(15);
    textLB.textColor = SetColorRGBValue(0x999999, 1.0);
    textLB.textAlignment = NSTextAlignmentCenter;
    [_noDataView addSubview:textLB];
    textLB.text = @"当前没有任何图片哦~";
}
-(void)creatCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) collectionViewLayout:flow];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = SetColorRGBValue(0xF9F9F9, 1.0);
    _collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    
    //注册cell
    [_collectionView registerClass:[SelectImageCell class] forCellWithReuseIdentifier:LCSelectImageCellIden];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SelectImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LCSelectImageCellIden forIndexPath:indexPath];
    if (self.imgUrlArrs.count > indexPath.item) {
        if ([self.imgUrlArrs[indexPath.item] isKindOfClass:[UIImage class]]) {
            [cell setupSelectImageCellData:nil img:self.imgUrlArrs[indexPath.item]];
        }else{
            [cell setupSelectImageCellData:_imgUrlArrs[indexPath.item] img:nil];
        }
    }
    [cell setupStatusImg:(self.currentSelectedRow == indexPath.item)];
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgUrlArrs.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.currentSelectedRow = indexPath.item;
    [_collectionView reloadData];
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat imgWidth = (ScreenWidth-40)/3.0 - 0.000001;
//    LCLog(@"imgWidth----%f",imgWidth);//124.666667
    return CGSizeMake(imgWidth, imgWidth);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    //cell间上下
    return 10;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
